'   BrushMotor_24_tests.txt       14 Jun  2019
' CHIHAI MOTOR Gear ratio:1:4.4
' Motor Type: CHR-GM25-370

Load Brush_Motor.jpg
Speed 9

Button 1 Text Start
Button 2 Text "PMW 000"
Button 3 Text "PMW 250"
Button 4 Text "PMW 300"
Button 5 Text "PMW 400"
Button 6 Text "PMW 500"
Button 7 Text "PMW 600"
Button 8 Text Increase

Load Theremino_HAL\Theremino_HAL.exe
Load Theremino_HAL\Theremino_SlotViewer.exe
Load Theremino_HAL\Theremino_SignalScope.exe
Load Theremino_HAL\Theremino_AudioGenerator.exe

Key 5 			Gosub FarFomZero
Key Right		Gosub MoveRight
Key Left 		Gosub MoveLeft
Key Up 			Gosub MoveUp
Key Down 		Gosub MoveDown
Key Pageup		Gosub MoveUpper
Key Pagedown	Gosub MoveLower
Key M			Gosub EnableM

Label MainLoop
	Wait Seconds 0.01
	'		
	v1 = v1 + 1	
	If v1 > 50
		v1 = 0		
		Gosub ColorOff
	EndIf
	If v1 > 49
		Gosub green	
	EndIf		
	'	
	'If	Slot(6) > 500
	'	Gosub Start
	'EndIf	
Goto MainLoop


Label Start
	Slot(1) = 0		'stop motor
	v1 = 250   		'start motor move
	Gosub ResetCounter		
	Gosub ReadCounter ' Counter = V9
	v2 = v9 + 5000
	Label Loop	
		Wait Seconds 0.01
		v1 = v1 + 2
		Slot(1) = v1			
		Gosub readcounter ' Counter = V9
		' ------------------------------------- too much counts		
		If v2 <= v9
			Slot(1) = 0	
			Slot(13) = 0 
			v1 = 0			
			Return	
		EndIf
		Gosub Sounds
		Gosub Colors
		' ------------------------------------- too much current
		If Slot(4) > 8000		
			Slot(1) = 0			
			Gosub Red
			v1 = 0			
			Return
		EndIf
	Goto Loop
Return



Label "PMW 000"
	Slot(1) = 0	
Return

Label "PMW 250"
	Slot(1) = 200	
Return

Label  "PMW 300"
	Slot(1) = 300	
Return

Label  "PMW 400"
	Slot(1) = 400	
Return

Label  "PMW 500"
	Slot(1) = 500	
Return	

Label  "PMW 600"
	Slot(1) = 600	
Return

Label  Increase
	Slot(1) = 1000	
Return	
'---------------------------------------------------------------------------------
'----- subroutine area -----------------------------------------------------------
'---------------------------------------------------------------------------------
Label Red
	Slot(7) = 1000		'red
	Slot(8) = 0			'yellow
	Slot(9) = 0			'green
Return

Label Yellow
	Slot(7) = 0			'red
	Slot(8) = 1000		'yellow
	Slot(9)	= 0			'green
Return

Label Green
	Slot(7) = 0			'red
	Slot(8) = 0			'yellow
	Slot(9) = 1000		'green
Return

Label ColorOff
	Slot(7) = 0			'red
	Slot(8) = 0			'yellow
	Slot(9) = 0			'green
Return


' -------------------------------------------------------------
'  The counter is V9 -  USE V9 IN YOUR CODE 
'  Do not use v7 and v8 in your code
' -------------------------------------------------------------
Label ReadCounter
	v8 = Slot(11)   	' new hardware value 
	v9 = v9 + (v8 - v7)	' update the counter (V9) with the difference
	If v8 < v7 - 16387	' If new hardware value < old hardware value - 1/4 range
		v9 = v9 + 65536	' then increase the counter (V9)	
	EndIf		
	v7 = v8				' old hardware value = new hardware value	
Return

Label ResetCounter
	v9 = 0   		' User counter value (V9)
	v8 = Slot(11)   ' new hardware value	
	v7 = v8   		' old hardware value
Return	

Label Sounds
	If Slot(4) > 3500
		Slot(13) = 500 + (Slot(4) - 3500) * 0.5
	Else
		Slot(13) = 0		
	EndIf	
Return

Label Colors
	Select True	
		Case  Slot(4) > 6000
			Gosub Red	
		Case  Slot(4) > 3500
			Gosub Yellow			
		CaseElse  
			Gosub Green
	EndSelect
Return




'====== DUMMY AREA ===============================================================

'====== TO DO AREA ===============================================================

'WATCH Slot(1)
'WATCH Slot(2)
'WATCH Slot(11)
'WATCH v1
'WATCH v2
'WATCH v9
'WATCH Slot(4)
'EXEC Slot(2) = 0
